 /*
  * CLASS IMAGE PLATE
  * =================
  * This manages the plate that is used to create and mantain the resulting
  * image.
  */
package rayTracer;

public class ImagePlate {
	private Pixel[] pixelsArray;	
	public int width;
	public int height;
	
	/*
	 * If no info for the pixels are given, the plate is filled with 
	 * the color WHITE (default color for the pixel creation)
	 * Is is mandatory to define width and height at the moment of 
	 * creation. Try to change width and height after that, is not
	 * allowed because we can not say how the pixels should be 
	 * redistributed on the new image 
	 */
	public ImagePlate( int width, int height ){
		this.width = width;
		this.height = height;
		
		pixelsArray = new Pixel[width*height];
		for( int i=0 ; i< pixelsArray.length ; i++ )
			pixelsArray[i] = new Pixel();
	}
	
	public ImagePlate( int width, int height, Pixel[] pixels ){
		this.width = width;
		this.height = height;
		pixelsArray = pixels;
	}	
	/*
	public int getWidth(){
		return width;
	}
	public int getHeight(){
		return height;
	}
	*/
	/*
	 * This return a Deep Copy of the array of pixels 
	 */
	public Pixel[] getPixelsArray(){
		Pixel[] ans =  pixelsArray.clone();
		for( int i=0 ; i< ans.length ; i++ )
			ans[i] = (Pixel) pixelsArray[i].clone();
		return ans;
	}
	
	/*
	 * This creates and stores a Deep Copy of the array parameter
	 */
	public void setPixelsArray( Pixel[] arr ){
		pixelsArray = arr.clone();
		for( int i=0 ; i< pixelsArray.length ; i++ )
			pixelsArray[i] = (Pixel) arr[i].clone();
	}
	
	/*
	 * Given the coordinates (x, y) in the matrix, the Pixel value
	 * overwrites the previous info
	 */
	public void  setPixel( int x, int y, Pixel value){
		int index = calculateIndex(x, y);
		pixelsArray[index] = (Pixel)value.clone();
	}
	
	/*
	 * Get the Pixel info in the coordinates (x,y) inise the matrix
	 */
	public Pixel getPixel( int x, int y ){
		int index = calculateIndex(x, y);
		return (Pixel)pixelsArray[index].clone();
	}
	
	public String toString(){
		StringBuffer str = new StringBuffer();
		
		str.append("[ Width = " + width + " ] [ Height = " + height + " ]");
		return str.toString();
	}
	
	/*
	 * Private method that helps translate the matrix ndexes style
	 * to the unique index used for the internal array
	 */
	private int calculateIndex( int x , int y ){
		return y*width + x ;
	}
}	
